package util;

import java.text.ParseException;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.swing.text.MaskFormatter;

public class CampoData extends javax.swing.JFormattedTextField{

	private MaskFormatter mask;

	public CampoData(){
		super(Utils.getFormatter());
	}

	public String campoObrigatorio(String rotulo) {
		return Utils.campoObrigatorio(this, rotulo);
	}

	public Date getDate() {
		try {
			Date valor = Utils.string2Date(this.getText());
			valor = new Date(valor.getTime() + new Date().getTime() % (1000*60*60*24));
			return valor;
		}
		catch (Exception e) {
			e.printStackTrace(); 
			return null;
		}
	}

	public int getDia() {
		GregorianCalendar calendario = new GregorianCalendar();
		calendario.setTime(getDate());
		return calendario.get(GregorianCalendar.DATE);
	}

	public int getMes() {
		GregorianCalendar calendario = new GregorianCalendar();
		calendario.setTime(getDate());
		return calendario.get(GregorianCalendar.MONTH) + 1;
	}

	public int getAno() {
		GregorianCalendar calendario = new GregorianCalendar();
		calendario.setTime(getDate());
		return calendario.get(GregorianCalendar.YEAR);
	}

	public void setText(Date data) {
		if(data==null) {
			setText("");
		}
		else {
			setText(Utils.date2String(data));
		}		
	}
}
